import time
from selenium import webdriver

from config.constant import IMPLICITLY
from handle.handle_log import log


class Base(object):

    """
    基础元素封装
    """

    def __init__(self, driver):
        self.driver = driver

    def find_element(self, *args):

        """
        单元素定位的重装
        :param args:
        :return:
        """
        # 设置显示等待
        # driver

        # 设置隐式等待
        self.driver.implicitly_wait(IMPLICITLY)
        try:
            # 返回定位对
            return self.driver.find_element(*args)
        except Exception as e:
            log.error(e)

    def find_elements(self, *args):

        """
        多单元素定位的重装
        :param args:
        :return:
        """
        # 设置显示等待
        # driver

        # 设置隐式等待
        self.driver.implicitly_wait(IMPLICITLY)
        try:
            # 返回定位对象
            return self.driver.find_elements(*args)
        except Exception as e:
            log.error(e)


    def to_iframe(self, ele):

        """
        进入iframe
        :param ele:
        :return:
        """
        self.driver.implicitly_wait(IMPLICITLY)
        try:
            # 进入iframe
            self.driver.switch_to.frame(ele)
        except Exception as e:
            log.error(e)

    def to_parentiframe(self):
        """
        回退到父iframe
        :return:
        """
        self.driver.implicitly_wait(20)
        self.driver.switch_to.parent_frame()

    def to_content(self):
        """
        回退到默认
        :return:
        """
        self.driver.implicitly_wait(20)
        self.driver.switch_to.default_content()

    def alert_accept(self):
        """
        切换弹窗并接受
        :return:
        """
        time.sleep(1)
        alert = self.driver.switch_to.alert
        text = alert.text
        alert.accept()
        return text

    def alert_dismiss(self):
        """
        切换弹窗
        :return:
        """
        time.sleep(1)
        alert = self.driver.switch_to.alert
        text = alert.text
        alert.dismiss()
        return text

    def alert_sendkey(self, string):
        """
        传值弹窗
        :param string:
        :return:
        """
        time.sleep(1)
        alert = self.driver.switch_to.alert
        alert.send_keys(string)
        alert.accept()

    def exe_script(self, string):
        """
        执行js脚本
        :param string:
        :return:
        """
        self.driver.execute_script(string)

    def swith_windows(self, num):
        """
        切换窗口
        :param num:
        :return:
        """
        handles = self.driver.window_handles
        self.driver.switch_to.window(handles[num])
        text = self.driver.title
        return text

    @property
    def driver_(self):
        """
        提供给外部使用的driver
        :return:
        """
        return self.driver

    def back(self):
        """
        回退
        :return:
        """
        self.driver.back()

    def forward(self):
        """
        前进
        :return:
        """
        self.driver.forword()

    def refresh(self):
        """
        刷新
        :return:
        """
        self.driver.refresh()

    def check_click(self, *args):
        """
        判断元素是否可点击
        :param args:
        :return:
        """
        self.driver.implicitly_wait(IMPLICITLY)
        ele = self.find_element(*args)
        if ele.is_enabled() == True:
            return ele
        else:
            log.error("元素不可点击")

    def open_url(self, url):
        """
        打开url
        :return:
        """
        return self.driver.get(url)

    def close(self):
        """
        关闭页面
        :return:
        """
        self.driver.close()

    def quit(self):
        """
        推出浏览器
        :return:
        """
        self.driver.quit()




